package com.ruoyi.subsidy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.NumCreateUtils;
import com.ruoyi.subsidy.domain.bo.SysPlantingPriceBo;
import com.ruoyi.subsidy.domain.bo.SysStrativeAreaBo;
import com.ruoyi.subsidy.domain.bo.SysSubsidyMachineBo;
import com.ruoyi.subsidy.domain.bo.SysSubsidyPlantingBo;
import com.ruoyi.subsidy.domain.vo.SysPlantingPriceVo;
import com.ruoyi.subsidy.domain.vo.SysStrativeAreaVo;
import com.ruoyi.subsidy.mapper.SysSubsidyMachineMapper;
import com.ruoyi.subsidy.mapper.SysSubsidyPlantingMapper;
import com.ruoyi.subsidy.service.ISysPlantingPriceService;
import com.ruoyi.subsidy.service.ISysStrativeAreaService;
import com.ruoyi.subsidy.service.ISysSubsidyMachineService;
import com.ruoyi.subsidy.service.ISysSubsidyPlantingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 补贴申请Service业务层处理
 *
 * @author nbacheng
 * @date 2025-08-07
 */
@RequiredArgsConstructor
@Service
public class SysSubsidyPlantingServiceImpl implements ISysSubsidyPlantingService {

    private final SysSubsidyPlantingMapper baseMapper;

    private final ISysPlantingPriceService iSysPlantingPriceService;

    //iSysPlantingPriceService.queryList(bo)

    private final ISysStrativeAreaService iSysStrativeAreaService;

    private final NumCreateUtils numCreateUtils;

    /**
     * 查询补贴申请
     */
    @Override
    public SysSubsidyPlantingBo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public SysSubsidyPlantingBo queryByPlantingBo(SysSubsidyPlantingBo bo) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<SysSubsidyPlantingBo>().eq(SysSubsidyPlantingBo::getAppNum, bo.getAppNum()));
    }

    @Override
    public List<String> queryDadouType(SysSubsidyPlantingBo bo) {
        return baseMapper.queryDadouType(bo);
    }



    /**
     * 查询补贴申请列表
     */
    @Override
    public TableDataInfo<SysSubsidyPlantingBo> queryPageList(SysSubsidyPlantingBo bo, PageQuery pageQuery) {
        //LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        // Page<SysSubsidyPlantingBo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        SysStrativeAreaBo boArea = new SysStrativeAreaBo();
        //boArea.setVillageName(bo.getVillageName());
        //List<SysStrativeAreaVo> areaVos = iSysStrativeAreaService.queryList(boArea);
        //String townName = areaVos.stream().map(t -> t.getTownName()).f  indFirst().get();
        //bo.setTownName(townName);
        bo.setStraName("滴道区");
        boArea.setTownName(bo.getTownName());
        Integer cunTol = baseMapper.selectByPlanBoCun(bo);
        Integer xiTol = baseMapper.selectByPlanBoXi(bo);
        Integer xiaxiacun = iSysStrativeAreaService.queryTotolByTown(boArea);
        LoginUser loginUser = LoginHelper.getLoginUser();
        String key = loginUser.getRoles().get(0).getRoleKey();
        TableDataInfo<SysSubsidyPlantingBo> table = new TableDataInfo<>();
        SysPlantingPriceBo pri = new SysPlantingPriceBo();
        List<SysPlantingPriceVo> listpri = iSysPlantingPriceService.queryList(pri);
        BigDecimal yumi = BigDecimal.ZERO;
        BigDecimal dadou = BigDecimal.ZERO;
        BigDecimal daogudixia = BigDecimal.ZERO;
        BigDecimal daogudibiao = BigDecimal.ZERO;
        BigDecimal gaodan = BigDecimal.ZERO;
        BigDecimal gaoyou = BigDecimal.ZERO;
        BigDecimal genlidili = BigDecimal.ZERO;
        BigDecimal gendilunzuo= BigDecimal.ZERO;
        if ("2".equals(bo.getIsprice()) && CollectionUtils.isEmpty(listpri)) {
            return table;
        }
        if ("district".equals(key)) {
            bo.setNode(3);
            table = qureyBytotalPlaneBoq(bo);
        }
        if ("town".equals(key)) {
            if ("审核中".equals(bo.getStatusShow())) {
                bo.setNode(3);
            }
            if ("待处理".equals(bo.getStatusShow())) {
                bo.setNode(2);
                bo.setStatus("审核中");
            }
            if ("异常".equals(bo.getStatusShow())) {
                bo.setNode(2);
                bo.setStatus("异常");
            }
            if ("已处理".equals(bo.getStatusShow())) {
                bo.setNode(2);
                bo.setStatus("已处理");
            }
            if (StringUtils.isEmpty(bo.getStatusShow())) {
                bo.setNode(23);
              //  bo.setStatus("已处理");
            }

            table = qureyBytotalPlanBox(bo);
        }
        if ("village".equals(key)) {
            if ("待处理".equals(bo.getStatusShow())) {
                bo.setNode(1);
            }
            if ("异常".equals(bo.getStatusShow())) {
                bo.setNode(1);
                bo.setStatus("异常");
            }
            if ("审核中".equals(bo.getStatusShow())) {
                bo.setNode(23);
            }
            table = qureyBytotalPlanBoc(bo);
            //bo.setNodeName(boo.getTownName());
        }

        for (SysPlantingPriceVo vo : listpri) {
            switch (vo.getType()) {
                case "玉米补贴":
                    yumi = new BigDecimal(vo.getPrice());
                    break;
                case "大豆补贴":
                    dadou = new BigDecimal(vo.getPrice());
                    break;
                case "稻谷地下水补贴":
                    daogudixia = new BigDecimal(vo.getPrice());
                    break;
                case "稻谷地表水补贴":
                    daogudibiao = new BigDecimal(vo.getPrice());
                    break;
                case "高蛋白大豆补贴":
                    gaodan = new BigDecimal(vo.getPrice());
                    break;
                case "高油大豆补贴":
                    gaoyou = new BigDecimal(vo.getPrice());
                    break;
                case "耕地地力补贴":
                    genlidili = new BigDecimal(vo.getPrice());
                    break;
                case "耕地轮作补贴":
                    gendilunzuo = new BigDecimal(vo.getPrice());
                    break;
                default:
                    // 其他类型不做处理
                    break;
            }
        }
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        Page<SysSubsidyPlantingBo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<SysSubsidyPlantingBo> list = result.getRecords();
        for (SysSubsidyPlantingBo e : list) {
            if ("2".equals(bo.getIsprice())) {
                if ("village".equals(key)) {
                    if ("1".equals(String.valueOf(e.getNode()))) {
                        e.setStatusShow("待处理");
                    } else if ("已完成".equals(e.getStatus())) {
                        e.setStatusShow("已完成");
                    } else {
                        e.setStatusShow("审核中");
                    }
                }
                if ("town".equals(key)) {
                    if ("2".equals(String.valueOf(e.getNode()))) {
                        e.setStatusShow("待处理");
                    } else if ("已完成".equals(e.getStatus())) {
                        e.setStatusShow("已完成");
                    } else {
                        e.setStatusShow("审核中");
                    }
                }
                if ("district".equals(key)) {
                    if ("已完成".equals(e.getStatus())) {
                        e.setStatusShow("已完成");
                    } else {
                        e.setStatusShow("待处理");
                    }
                }
            } else {
                if (StringUtils.isEmpty(bo.getStatusShow())) {
                    if ("village".equals(key)) {
                        if ("异常".equals(e.getStatus())) {
                            e.setStatusShow("异常");
                        } else {
                            if ("1".equals(String.valueOf(e.getNode()))) {
                                e.setStatusShow("待处理");
                            } else {
                                e.setStatusShow("审核中");
                            }
                        }
                    }
                    if ("town".equals(key)) {
                        if ("异常".equals(e.getStatus())) {
                            e.setStatusShow("异常");
                        } else if ("已处理".equals(e.getStatus())) {
                            e.setStatusShow("已处理");
                        } else {
                            if ("2".equals(String.valueOf(e.getNode()))) {
                                e.setStatusShow("待处理");
                            } else {
                                e.setStatusShow("审核中");
                            }
                        }
                    }
                    if ("district".equals(key)) {
                        e.setStatusShow("待处理");
                    }
                } else {
                    e.setStatusShow(bo.getStatusShow());
                }
            }
            e.setYumiareaAmountPri(yumi.multiply(e.getYumiareaAmount()));
            e.setDadouareaAmountPri(dadou.multiply(e.getDadouareaAmount()));
            //e.setDaoguheareaAmountPri(daogu.multiply(e.getDaoguheareaAmount()));
            e.setDaogudibiareaAmountPri(daogudibiao.multiply(e.getDaogudibiareaAmount()));
            e.setDaogudixiaareaAmountPri(daogudixia.multiply(e.getDaogudixiaareaAmount()));
            e.setDaoguheareaAmountPri(e.getDaogudibiareaAmountPri().add(e.getDaogudixiaareaAmountPri()));

            e.setAreaAmountPri(e.getYumiareaAmountPri().add(e.getDadouareaAmountPri().add(e.getDaoguheareaAmountPri())));
            e.setAreaAmountdouGaoyouPri(gaoyou.multiply(e.getAreaAmountdouGaoyou()));
            e.setAreaAmountdouGaodanbaiPri(gaodan.multiply(e.getAreaAmountdouGaodanbai()));
            e.setAreaAmountdou(e.getAreaAmountdouGaoyou().add(e.getAreaAmountdouGaodanbai()));
            e.setAreaAmountdouPri(e.getAreaAmountdouGaoyouPri().add(e.getAreaAmountdouGaodanbaiPri()));
            e.setFarmlandareaAmountPri(genlidili.multiply(e.getFarmlandareaAmount()));
            e.setCurallonepre(gendilunzuo.multiply(e.getCurallone()));
            e.setCuralltwopre(gendilunzuo.multiply(e.getCuralltwo()));
            e.setCurallthreepre(gendilunzuo.multiply(e.getCurallthree()));
            e.setCurallfourpre(gendilunzuo.multiply(e.getCurallfour()));
            e.setCropallpre(gendilunzuo.multiply(e.getCropall()));
        }
        TableDataInfo<SysSubsidyPlantingBo> table1 = qureyBytotalArea(bo);
        TableDataInfo<SysSubsidyPlantingBo> tableNew = table1.build(result);
        //TableDataInfo<SysSubsidyPlantingBo> table1=qureyBytotalArea(bo);
        tableNew.setShenhetotal(null == table ? 0 : table.getShenhetotal());
        tableNew.setDaichutotal(null == table ? 0 : table.getDaichutotal());
        tableNew.setYichangtotal(null == table ? 0 : table.getYichangtotal());
        tableNew.setYichulitotal(null == table ? 0 : table.getYichulitotal());
        tableNew.setQuanbutotal(null == table ? 0 : table.getQuanbutotal());
        tableNew.setXitiFlag(cunTol == xiaxiacun);
        tableNew.setQutiFlag(xiTol == 8);
        if (null != table1) {
            tableNew.setScbycuntotal(table1.getScbycuntotal());
            tableNew.setScbyxitotal(table1.getScbyxitotal());
            tableNew.setScqutbyotal(table1.getScqutbyotal());

            tableNew.setYubycuntotal(table1.getYubycuntotal());
            tableNew.setYubycuntotalPri(yumi.multiply(table1.getYubycuntotal()));
            tableNew.setYubyxitotal(table1.getYubyxitotal());
            tableNew.setYubyxitotalPri(yumi.multiply(table1.getYubyxitotal()));
            tableNew.setYubyqutotal(table1.getYubyqutotal());
            tableNew.setYubyqutotalPri(yumi.multiply(table1.getYubyqutotal()));

            tableNew.setDadoubycuntotal(table1.getDadoubycuntotal());
            tableNew.setDadoubycuntotalPri(dadou.multiply(table1.getDadoubycuntotal()));
            tableNew.setDadoubyxitotal(table1.getDadoubyxitotal());
            tableNew.setDadoubyxitotalPri(dadou.multiply(table1.getDadoubyxitotal()));
            tableNew.setDadoubyqutotal(table1.getDadoubyqutotal());
            tableNew.setDadoubyqutotalPri(dadou.multiply(table1.getDadoubyqutotal()));


            tableNew.setGudibiaobycuntotal(table1.getGudibiaobycuntotal());
            tableNew.setGudibiaobycuntotalPri(daogudibiao.multiply(table1.getGudibiaobycuntotal()));
            tableNew.setGudibiaobyxitotal(table1.getGudibiaobyxitotal());
            tableNew.setGudibiaobyxitotalPri(daogudibiao.multiply(table1.getGudibiaobyxitotal()));
            tableNew.setGudibiaobyqutotal(table1.getGudibiaobyqutotal());
            tableNew.setGudibiaobyqutotalPri(daogudibiao.multiply(table1.getGudibiaobyqutotal()));

            tableNew.setGudixiabycuntotal(table1.getGudixiabycuntotal());
            tableNew.setGudixiabycuntotalPri(daogudixia.multiply(table1.getGudixiabycuntotal()));
            tableNew.setGudixiabyxitotal(table1.getGudixiabyxitotal());
            tableNew.setGudixiabyxitotalPri(daogudixia.multiply(table1.getGudixiabyxitotal()));
            tableNew.setGudixiabyqutotal(table1.getGudixiabyqutotal());
            tableNew.setGudixiabyqutotalPri(daogudixia.multiply(table1.getGudixiabyqutotal()));

            tableNew.setGubycuntotal(table1.getGubycuntotal());
            tableNew.setGubycuntotalPri(tableNew.getGudibiaobycuntotalPri().add(tableNew.getGudixiabycuntotalPri()));
            tableNew.setGubyxitotal(table1.getGubyxitotal());
            tableNew.setGubyxitotalPri(tableNew.getGudibiaobyxitotalPri().add(tableNew.getGudixiabyxitotalPri()));
            tableNew.setGubyqutotal(table1.getGubyqutotal());
            tableNew.setGubyqutotalPri(tableNew.getGudibiaobyqutotalPri().add(tableNew.getGudixiabyqutotalPri()));


            tableNew.setScbycuntotalPri(tableNew.getYubycuntotalPri().add(tableNew.getDadoubycuntotalPri().add(tableNew.getGubycuntotalPri())));
            tableNew.setScbyxitotalPri(tableNew.getYubyxitotalPri().add(tableNew.getDadoubyxitotalPri().add(tableNew.getGubyxitotalPri())));
            tableNew.setScqutbyotalPri(tableNew.getYubyqutotalPri().add(tableNew.getDadoubyqutotalPri().add(tableNew.getGubyqutotalPri())));

            tableNew.setFarmlandbycuntotal(table1.getFarmlandbycuntotal());
            tableNew.setFarmlandbycuntotalPri(genlidili.multiply(table1.getFarmlandbycuntotal()));
            tableNew.setFarmlandbyxitotal(table1.getFarmlandbyxitotal());
            tableNew.setFarmlandbyxitotalPri(genlidili.multiply(table1.getFarmlandbyxitotal()));
            tableNew.setFarmlandbyqutotal(table1.getFarmlandbyqutotal());
            tableNew.setFarmlandbyqutotalPri(genlidili.multiply(table1.getFarmlandbyqutotal()));

            tableNew.setYoudouGaoyoubycuntotal(table1.getYoudouGaoyoubycuntotal());
            tableNew.setYoudouGaoyoubycuntotalPri(gaoyou.multiply(table1.getYoudouGaoyoubycuntotal()));
            tableNew.setYoudouGaoyoubyxitotal(table1.getYoudouGaoyoubyxitotal());
            tableNew.setYoudouGaoyoubyxitotalPri(gaoyou.multiply(table1.getYoudouGaoyoubyxitotal()));
            tableNew.setYoudouGaoyoubyqutotal(table1.getYoudouGaoyoubyqutotal());
            tableNew.setYoudouGaoyoubyqutotalPri(gaoyou.multiply(table1.getYoudouGaoyoubyqutotal()));

            tableNew.setYoudouGaodanbaibycuntotal(table1.getYoudouGaodanbaibycuntotal());
            tableNew.setYoudouGaodanbaibycuntotalPri(gaodan.multiply(table1.getYoudouGaodanbaibycuntotal()));
            tableNew.setYoudouGaodanbaibyxitotal(table1.getYoudouGaodanbaibyxitotal());
            tableNew.setYoudouGaodanbaibyxitotalPri(gaodan.multiply(table1.getYoudouGaodanbaibyxitotal()));
            tableNew.setYoudouGaodanbaibyqutotal(table1.getYoudouGaodanbaibyqutotal());
            tableNew.setYoudouGaodanbaibyqutotalPri(gaodan.multiply(table1.getYoudouGaodanbaibyqutotal()));

            tableNew.setYoudoubycuntotal(tableNew.getYoudouGaoyoubycuntotal().add(tableNew.getYoudouGaodanbaibycuntotal()));
            tableNew.setYoudoubycuntotalPri(tableNew.getYoudouGaoyoubycuntotalPri().add(tableNew.getYoudouGaodanbaibycuntotalPri()));
            tableNew.setYoudoubyxitotal(tableNew.getYoudouGaoyoubyxitotal().add(tableNew.getYoudouGaodanbaibyxitotal()));
            tableNew.setYoudoubyxitotalPri(tableNew.getYoudouGaoyoubyxitotalPri().add(tableNew.getYoudouGaodanbaibyxitotalPri()));
            tableNew.setYoudoubyqutotal(tableNew.getYoudouGaoyoubyqutotal().add(tableNew.getYoudouGaodanbaibyqutotal()));
            tableNew.setYoudoubyqutotalPri(tableNew.getYoudouGaoyoubyqutotalPri().add(tableNew.getYoudouGaodanbaibyqutotalPri()));


            tableNew.setCurallonebycun(table1.getCurallonebycun());
            tableNew.setCuralloneprebycun(gendilunzuo.multiply(table1.getCurallonebycun()));
            tableNew.setCurallonebyxi(table1.getCurallonebyxi());
            tableNew.setCuralloneprebyxi(gendilunzuo.multiply(table1.getCurallonebyxi()));
            tableNew.setCurallonebyqu(table1.getCurallonebyqu());
            tableNew.setCuralloneprebyqu(gendilunzuo.multiply(table1.getCurallonebyqu()));

            tableNew.setCuralltwobycun(table1.getCuralltwobycun());
            tableNew.setCuralltwoprebycun(gendilunzuo.multiply(table1.getCuralltwobycun()));
            tableNew.setCuralltwobyxi(table1.getCuralltwobyxi());
            tableNew.setCuralltwoprebyxi(gendilunzuo.multiply(table1.getCuralltwobyxi()));
            tableNew.setCuralltwobyqu(table1.getCuralltwobyqu());
            tableNew.setCuralltwoprebyqu(gendilunzuo.multiply(table1.getCuralltwobyqu()));

            tableNew.setCurallthreebycun(table1.getCurallthreebycun());
            tableNew.setCurallthreeprebycun(gendilunzuo.multiply(table1.getCurallthreebycun()));
            tableNew.setCurallthreebyxi(table1.getCurallthreebyxi());
            tableNew.setCurallthreeprebyxi(gendilunzuo.multiply(table1.getCurallthreebyxi()));
            tableNew.setCurallthreebyqu(table1.getCurallthreebyqu());
            tableNew.setCurallthreeprebyqu(gendilunzuo.multiply(table1.getCurallthreebyqu()));

            tableNew.setCurallfourbycun(table1.getCurallfourbycun());
            tableNew.setCurallfourprebycun(gendilunzuo.multiply(table1.getCurallfourbycun()));
            tableNew.setCurallfourbyxi(table1.getCurallfourbyxi());
            tableNew.setCurallfourprebyxi(gendilunzuo.multiply(table1.getCurallfourbyxi()));
            tableNew.setCurallfourbyqu(table1.getCurallfourbyqu());
            tableNew.setCurallfourprebyqu(gendilunzuo.multiply(table1.getCurallfourbyqu()));

            tableNew.setCropallbycun(table1.getCropallbycun());
            tableNew.setCropallprebycun(gendilunzuo.multiply(table1.getCropallbycun()));
            tableNew.setCropallbyxi(table1.getCropallbyxi());
            tableNew.setCropallprebyxi(gendilunzuo.multiply(table1.getCropallbyxi()));
            tableNew.setCropallbyqu(table1.getCropallbyqu());
            tableNew.setCropallprebyqu(gendilunzuo.multiply(table1.getCropallbyqu()));
        }
        return tableNew;
    }


    public TableDataInfo<SysSubsidyPlantingBo> qureyBytotalArea(SysSubsidyPlantingBo bo) {
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);

        //        //Page<SysSubsidyMachineBo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // List<SysSubsidyMachineBo> list=result.getRecords();
        //  list.forEach(e->{
        //  });
        return baseMapper.qureyBytotalArea(bo);
    }

    public TableDataInfo<SysSubsidyPlantingBo> qureyBytotalPlanBoc(SysSubsidyPlantingBo bo) {
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);

        //        //Page<SysSubsidyMachineBo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // List<SysSubsidyMachineBo> list=result.getRecords();
        //  list.forEach(e->{
        //  });
        return baseMapper.qureyBytotalPlanBoc(bo);
    }


    public TableDataInfo<SysSubsidyPlantingBo> qureyBytotalPlanBox(SysSubsidyPlantingBo bo) {
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        return baseMapper.qureyBytotalPlanBox(bo);
    }


    public TableDataInfo<SysSubsidyPlantingBo> qureyBytotalPlaneBoq(SysSubsidyPlantingBo bo) {
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        return baseMapper.qureyBytotalPlaneBoq(bo);
    }

    /**
     * 查询补贴申请列表
     */
    @Override
    public List<SysSubsidyPlantingBo> queryList(SysSubsidyPlantingBo bo) {
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }
    @Override
    public List<SysSubsidyPlantingBo> queryListChage(SysSubsidyPlantingBo bo) {
       // LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        return baseMapper.queryListChage(bo);
    }

    @Override
    public List<SysSubsidyPlantingBo> getAmountGroupCun(SysSubsidyPlantingBo bo) {
        // LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        return baseMapper.getAmountGroupCun(bo);
    }

    @Override
    public List<SysSubsidyPlantingBo> getAmountGroupXi(SysSubsidyPlantingBo bo) {
        // LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = buildQueryWrapper(bo);
        return baseMapper.getAmountGroupXi(bo);
    }

    private LambdaQueryWrapper<SysSubsidyPlantingBo> buildQueryWrapper(SysSubsidyPlantingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSubsidyPlantingBo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAppType()), SysSubsidyPlantingBo::getAppType, bo.getAppType());
        lqw.eq(StringUtils.isNotBlank(bo.getAppNum()), SysSubsidyPlantingBo::getAppNum, bo.getAppNum());
        lqw.eq(StringUtils.isNotBlank(bo.getAppyBy()), SysSubsidyPlantingBo::getAppyBy, bo.getAppyBy());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), SysSubsidyPlantingBo::getIdCard, bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), SysSubsidyPlantingBo::getBankCard, bo.getBankCard());
        lqw.eq(StringUtils.isNotBlank(bo.getVillageName()), SysSubsidyPlantingBo::getVillageName, bo.getVillageName());
        lqw.eq(StringUtils.isNotBlank(bo.getTownName()), SysSubsidyPlantingBo::getTownName, bo.getTownName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysSubsidyPlantingBo::getStatus, bo.getStatus());
        if ("23".equals(String.valueOf(bo.getNode()))) {
            lqw.in(bo.getNode() != null, SysSubsidyPlantingBo::getNode, Arrays.asList(2, 3));
        } else {
            lqw.eq(bo.getNode() != null, SysSubsidyPlantingBo::getNode, bo.getNode());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getYear()), SysSubsidyPlantingBo::getYear, bo.getYear());
        lqw.eq(StringUtils.isNotBlank(bo.getSubmitBy()), SysSubsidyPlantingBo::getSubmitBy, bo.getSubmitBy());
        lqw.eq(bo.getSubmitDate() != null, SysSubsidyPlantingBo::getSubmitDate, bo.getSubmitDate());
        lqw.eq(bo.getIsprice() != null, SysSubsidyPlantingBo::getIsprice, bo.getIsprice());
        if ("玉米补贴".equals(bo.getExportType())) {
            lqw.gt(SysSubsidyPlantingBo::getYumiareaAmount, 0);
        }
        if ("大豆补贴".equals(bo.getExportType())) {
            lqw.gt(SysSubsidyPlantingBo::getDadouareaAmount, 0);
        }
        if ("稻谷补贴".equals(bo.getExportType())) {
            lqw.gt(SysSubsidyPlantingBo::getDaoguheareaAmount, 0);
        }
        return lqw;
    }


    /**
     * 新增补贴申请
     */
    @Override
    public String insertByBo(SysSubsidyPlantingBo bo) {
        //SysSubsidyPlantingBo add = BeanUtil.toBean(bo, SysSubsidyPlantingBo.class);
        validEntityBeforeSave(bo);
        String num = numCreateUtils.generateOrderNumber();
        bo.setAppNum(num);
        bo.setStatus("待处理");
        bo.setNode(1);
        //List<
        boolean flag = baseMapper.insertSubsidyPlanting(bo) > 0;
        //if (flag) {
        //   bo.setId(add.getId());
        return num;
    }

    /**
     * 修改补贴申请
     */
    @Override
    public Boolean updateByBo(SysSubsidyPlantingBo bo) {
        SysSubsidyPlantingBo update = BeanUtil.toBean(bo, SysSubsidyPlantingBo.class);
        //validEntityBeforeSave(update);
        update.setStatus("待处理");
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 修改补贴申请
     */
    @Override
    public Boolean updateByPlanBo(SysSubsidyPlantingBo bo) {
        return baseMapper.updateByPlanBo(bo) > 0;
    }

    /**
     * 修改补贴申请
     */
    @Override
    public Boolean updateByPlanBoBohui(SysSubsidyPlantingBo bo) {
        return baseMapper.updateByPlanBoBohui(bo) > 0;
    }


    /**
     * 单个修改补贴申请
     */
    @Override
    public Boolean updateByPlanBoOne(SysSubsidyPlantingBo bo) {
        return baseMapper.updateByPlanBoOne(bo) > 0;
    }


    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSubsidyPlantingBo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除补贴申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
